<template>
	<view class="contant">
		<view class="contant_show">
			<view class="top_app">
				<h1 style="text-align: center;" v-if="isJiesuo">点击下方解锁资源</h1>
				<h2 style="text-align: center;">↓↓↓↓</h2>
				<h3 style="padding: 20rpx;border-bottom: 1px dashed #aaa;">今日资源</h3>
				<view class="middle" v-if="isJiesuo">
					余下部分已加密，已有13<span style="color: red;">***</span>666人领取
				</view>
				<!-- <u-button  type="primary">点击解锁资源</u-button> -->
				<view style="display: flex;justify-content: center;" v-if="isJiesuo">
					<button class="heartbeat-button" @click="onunhandledrejection1">点击观看广告</button>
					<button class="heartbeat-button1" @click="onunhandledrejection11('1')">点击解锁资源</button>
				</view>
				<view class="yijianfuzhi" v-if="!isJiesuo">
					<view style="color: green;text-align: center;padding: 20rpx;">
						解锁成功~感谢支持
					</view>
					<view style="padding: 20rpx;">
						<u-collapse>
							<u-collapse-item title="资源地址:" name="Docs guide">
								<text class="u-collapse-content">链接:{{urls}}</text>
							</u-collapse-item>
						</u-collapse>
					</view>
					<view class="">
						<u-button type="primary" @click="copyBtn()">一键复制</u-button>
					</view>
				</view>
			</view>
			<view class="contant_show_tishi" v-if="isJiesuo">
				<h3 style="text-align: center;color: red;">解锁说明:观看完成后点击解锁资源即可解锁</h3>
				<p>1:点击红色按钮跳转进入小程序</p>
				<p>2.点击观看视频广告,待广告播放完</p>
				<p>3.点击关闭广告,点击右上角小圆点关闭小程序</p>
				<p>4.返回本页面点击绿色按钮解锁资源</p>
			</view>
			<view class="contant_show_tishi" v-if="!isJiesuo">
				<h3 style="text-align: center;color: red;">宝子tips:</h3>
				<p>一键复制以后,到聊天窗口粘贴(复制以后到浏览器打开)</p>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				miniProgramUrl: '',
				isshow: true,
				isJiesuo: true,
				urls: 'https://pan.baidu.com/s/1ARzv-bZIQcIFYHYB77-W5g?pwd=n8iy',
				ip: '',
				miniProgramAppId: '',
				_id: '',
				miniProgramPath: '',
				datasname: '', //当前
				datasss: [],
				isPC: false,
				datass: {
					"name": "",
					"appid": "",
					"lianjie": "",
					"isDianji": false,
					"lianjieData": "",
					"ip": "",
					"date": "",
					"count": ""
				}
			};
		},
		onLoad(option) {
			this._id = option._id //用来获取绑定的链接地址
			this.onunhandledrejection11('')
		},
		mounted() {
			this.SaveData11('', 'get_lianjie')
			this.panduanPC()
		},
		methods: {
			onunhandledrejection11(data1) {
				let me = this
				//先获取ip
				uni.request({
					url: 'https://fc-mp-15664855-c6b0-43bb-ab42-9bad6993ccd7.next.bspapp.com/demoArt/get1', //仅为示例，并非真实接口地址。
					method: 'POST',
					data: {},
					header: {},
					success: (res) => {
						console.log(res.data);
						// this.isJiesuo = !this.isJiesuo
						this.isshow = true
						this.gettime()
						uni.showLoading({
							title: '加载中'
						});
						setTimeout(() => {
							uni.hideLoading();
						}, 2000)
						me.ip = res.data.data['x-real-ip']
						console.log(me.ip)
						uniCloud.callFunction({
							name: 'uni-id-cf',
							data: {
								action: 'get'
							},
							success: res => {
								this.datasname = res.result.data.find(item => {
									return item.ip == me.ip
								})
								this.miniProgramAppId = res.result.data[0].appid;
								this.miniProgramPath = '/pages/indexs/indexs';
								if (!this.datasname) {
									//没有新增过数据
									let data = {
										"appid": "无",
										"isDianji": false,
										"lianjieData": "无",
										"ip": me.ip,
										"date": this.gettime(),
										"count": 0
									}
									this.SaveData(data, 'add')
								} else {
									if (this.datasname.isDianji && this.datasname.count >= 1) {
										this.isJiesuo = false
										uni.showModal({
											title: '提示',
											content: '解锁成功',
											success: function(res) {
												if (res.confirm) {
													//需要更新下改状态
												} else if (res.cancel) {
													console.log('用户点击取消');
												}
											}
										});
										this.$forceUpdate()
										const parameter = this.datasname._id; //id
										this.miniProgramUrl =
											`weixin://dl/business/?appid=${this.miniProgramAppId}&path=${this.miniProgramPath}&parameter=${parameter}`;
										console.log(this.miniProgramUrl)
									} else {
										if (data1) {
											uni.showModal({
												title: '提示',
												content: '解锁失败',
												success: function(res) {
													if (res.confirm) {
														//需要更新下改状态
													} else if (res.cancel) {
														console.log('用户点击取消');
													}
												}
											});
										}
										const parameter = this.datasname._id; //id
										this.miniProgramUrl =
											`weixin://dl/business/?appid=${this.miniProgramAppId}&path=${this.miniProgramPath}&parameter=${parameter}`;
										console.log(this.miniProgramUrl)
									}
								}
								uni.hideLoading();
							},
							fail: err => {
								uni.hideLoading();
								console.error(err)
							}
						})
					}
				});
			},
			updateUser() {
				uniCloud.callFunction({
					name: 'uni-id-cf',
					data: {
						action: 'update',
						_id: this.ip,
						data: {
							isDianji: false
						}
					},
					success: res => {
						uni.setClipboardData({
							data: this.urls,
							success: function() {
								uni.showToast({
									title: '复制成功',
								});
								//需要将当前的变成false
								this.updateUser()
							},
							fail: function() {
								uni.showToast({
									title: '复制失败',
								});
							}
						});
					},
					fail: err => {
						console.error(err)
					}
				})
			},
			SaveData11(data1, name) {
				uniCloud.callFunction({
					name: 'uni-id-cf',
					data: {
						action: name,
						_id: this._id,
						data: {
							...data1
						}
					},
					success: res => {
						console.log(res.result)
						this.datasss = res.result.data
						// this.formdata.appid = res.result.data[0].appid
						// this.formdata.lianjie = res.result.data[0].lianjie
						// this.formdata.id = res.result.data[0]._id
						this.datasss.forEach(item => {
							if (item._id == this._id) {
								this.urls = item.lianjie
							}
						})
					},
					fail: err => {
						console.error(err)
					}
				})
			},
			SaveData(data1, name) {
				uniCloud.callFunction({
					name: 'uni-id-cf',
					data: {
						action: name,
						_id: name == "add" ? "" : this.datasname._id,
						data: {
							...data1
						}
					},
					success: res => {
						console.log(res.result)
						name == "add" ? "" : this.isshow = false
						this.datasname = data1
						if (name == 'add') {
							this.datasname._id = res.result.id
							const parameter = res.result._id; //id
							this.miniProgramUrl =
								`weixin://dl/business/?appid=${this.miniProgramAppId}&path=${this.miniProgramPath}&parameter=${parameter}`;
						} else {
							window.location.href = 'https://iphr.uiuin.cn/eJsIbC'
						}
					},
					fail: err => {
						console.error(err)
					}
				})
			},
			panduanPC() {
				const userAgent = navigator.userAgent.toLowerCase();
				if (/ipad|iphone|midp|rv:1.2.3.4|ucweb|android|windows ce|windows mobile/.test(userAgent)) {
					// 移动端
					this.isPC = false
				} else {
					// pc端
					this.isPC = true
				}
			},
			onunhandledrejection1() {
				//  this.isshow = !this.isshow
				if (this.isPC) {
					uni.showModal({
						title: '提示',
						content: '请使用手机端打开',
						success: function(res) {
							if (res.confirm) {} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
					return false
				}
				this.datasname.count += 1
				this.datasname.date = this.gettime()
				this.SaveData({
					count: this.datasname.count,
					date: this.datasname.date,
				}, 'update')
				uni.setStorageSync('clickRecords', this.datasname)
				// plus.runtime.openURL('https://ad.pdb2.com/l/1oD1TH3hbTckc234')
			},
			gettime() {
				//获取当前年月日
				var now = new Date();
				var year = now.getFullYear(); //得到年份
				var month = now.getMonth() + 1; //得到月份
				var date = now.getDate(); //得到日期
				return year + "年" + month + "月" + date + "日";
			},
			copyBtn: function() {
				this.updateUser()
			}
		}
	};
</script>


<style lang="scss">
	.contant {
		padding: 20rpx;
		box-sizing: border-box;
	}

	.middle {
		width: 100%;
		height: 140rpx;
		background-color: rgb(243, 244, 246);
		text-align: center;
		line-height: 140rpx;
		margin-bottom: 20rpx;
	}

	.top_app {
		border: 1px solid #eee;
		border-radius: 10rpx;
		padding: 20rpx;
	}

	.contant_show_tishi {
		width: 100%;
		padding: 20rpx;
		margin-top: 40rpx;
		background-color: rgb(243, 244, 246);
		border: 1px solid #eee;
		border-radius: 10rpx;
		box-sizing: border-box;

		p {
			line-height: 60rpx;
			color: rgb(2, 127, 255);
		}


	}

	.heartbeat-button {
		display: inline-block;
		position: relative;
		padding: 10px 20px;
		font-size: 16px;
		font-weight: bold;
		color: #fff;
		background-color: #f44336;
		border: none;
		border-radius: 5px;
		cursor: pointer;
		animation: heartbeat 1.5s infinite;
	}

	.heartbeat-button1 {
		display: inline-block;
		position: relative;
		padding: 10px 20px;
		font-size: 16px;
		font-weight: bold;
		color: #fff;
		background-color: green;
		border: none;
		border-radius: 5px;
		cursor: pointer;
		animation: heartbeat 1.5s infinite;
	}

	@keyframes heartbeat {
		0% {
			transform: scale(1);
		}

		25% {
			transform: scale(1.2);
		}

		50% {
			transform: scale(1);
		}

		75% {
			transform: scale(1.2);
		}

		100% {
			transform: scale(1);
		}
	}

	.heartbeat-button:before,
	.heartbeat-button:after {
		content: '';
		display: block;
		position: absolute;
		top: -10px;
		left: -10px;
		width: calc(100% + 20px);
		height: calc(100% + 20px);
		border-radius: 50%;
		opacity: 0;
		background-color: rgba(255, 255, 255, 0.3);
		z-index: -1;
		animation: heartbeat-before 1.5s infinite;
	}

	@keyframes heartbeat-before {
		0% {
			transform: scale(0);
			opacity: 0.5;
		}

		50% {
			opacity: 0;
		}

		100% {
			transform: scale(1.5);
			opacity: 0;
		}
	}

	.heartbeat-button:hover:before,
	.heartbeat-button:hover:after {
		animation-play-state: paused;
	}
</style>